package com.jic.manager.controller.manage.conf;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.conf.api.manage.RegionApi;
import com.jic.manager.entity.Region;
import com.jic.manager.entity.RegionProvince;
import com.jic.conf.request.RegionRequest;
import com.jic.conf.response.RegionResponse;
import com.jic.manager.service.RegionProvinceService;
import com.jic.manager.service.RegionService;
import com.jic.manager.utils.BeanUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/region")
public class RegionController implements RegionApi {

    @Resource
    private RegionService regionService;
    @Resource
    private RegionProvinceService regionProvinceService;

    @ApiOperation("销售区域分页查询")
    @PostMapping("/getRegionPageList")
    @Override
    public RestResult<PageResult<RegionResponse>> getRegionPageList(@RequestBody RegionRequest regionRequest) {
        Page page = new Page();
        page.setLimit(regionRequest.getLimit());
        page.setStart(regionRequest.getStart());
        PageResult<Region> regionPageResult = regionService.queryPage(new Region(), page);
        List<Region> regionList = regionPageResult.getRows();
        List<RegionResponse> res = new ArrayList<>();
        regionList.forEach(region -> {
            List<RegionProvince> regionProvinces = regionProvinceService.query(new RegionProvince() {{
                setRegionId(region.getId());
            }});
            RegionResponse regionResponse = new RegionResponse();
            try {
                BeanUtils.Copy(region,regionResponse);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (CollectionUtils.isEmpty(regionProvinces)) {
                res.add(regionResponse);
            }else{
                List<Long> cityIds = regionProvinces.stream().map(RegionProvince::getAddressId).collect(Collectors.toList());
                regionResponse.setCityIds(cityIds);
                res.add(regionResponse);
            }
        });
        return RestResult.success(new PageResult<RegionResponse>(){{
            setRows(res);
            setTotal(regionPageResult.getTotal());
        }});
    }

    @ApiOperation("销售区域删除")
    @PostMapping("/delRegionInfo")
    @Override
    public RestResult<String> delRegionInfo(@RequestBody RegionRequest regionRequest) {
        String ids = regionRequest.getIds();
        String[] split = ids.split(",");
        for(String id : split){
            regionService.delete(Long.valueOf(id));
            List<RegionProvince> regionProvinces = regionProvinceService.query(new RegionProvince() {{
                setRegionId(regionRequest.getId());
            }});
            regionProvinces.forEach(regionProvince -> {
                regionProvinceService.delete(regionProvince.getId());
            });
        }
        return RestResult.success("成功");
    }
//
//    @ApiOperation("查询所有选中的")
//    @PostMapping("/updateRegion")
//    @ResponseBody
//    @Override
//    public RestResult<List<RegionProvinceResponse>> getRegionProvinceList(RegionProvinceRequest regionProvinceRequest) {
//        try {
//            RegionProvince regionProvince = new RegionProvince();
//            BeanUtils.Copy(regionProvinceRequest,regionProvince);
//            List<RegionProvince> regionProvinces = regionProvinceService.query(regionProvince);
//            List<RegionProvinceResponse> regionProvinceResponses = new ArrayList<>();
//            regionProvinces.forEach(regionProvince1 -> {
//                RegionProvinceResponse regionProvinceResponse = new RegionProvinceResponse();
//                try {
//                    BeanUtils.Copy(regionProvince1,regionProvinceResponse);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                regionProvinceResponses.add(regionProvinceResponse);
//            });
//            return RestResult.success(regionProvinceResponses);
//        }catch (Exception e){
//            e.printStackTrace();
//            return RestResult.error("9999",e.getLocalizedMessage());
//        }
//    }

    @ApiOperation("修改销售区域")
    @PostMapping("/updateRegion")
    public RestResult<String> updateRegion(@RequestBody RegionRequest regionRequest) throws Exception {
        Region region = new Region();
        SysProvinceCityController.Copy(regionRequest,region);
        if (region.getId() != 0){
            //删除关联表
            List<RegionProvince> list = regionProvinceService.query(new RegionProvince() {{
                setRegionId(region.getId());
            }});
            list.forEach(regionProvince -> {
                regionProvinceService.delete(regionProvince.getId());
            });
            regionService.update(region);
        }else{
            regionService.insert(region);
        }
        String cityIds = regionRequest.getCityIds();
        if (StringUtils.isNotEmpty(cityIds)) {
            //新增关联表
            String[] split = cityIds.split(",");
            for (String cityId : split) {
                regionProvinceService.insert(new RegionProvince(){{
                    setRegionId(region.getId());
                    setAddressId(Long.valueOf(cityId));
                }});
            }
        }
        return RestResult.success("成功");
    }








}
